package com.seedwill.common.dozer;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * 日期转换支持
 * 
 * TODO 收集所有可能的转换
 * 
 * 
 * @author kdyi
 *
 */
public class DateTimeConverterSupport {
	public static Log log = LogFactory.getLog(DateTimeConverterSupport.class);
	public static Map<String, DateFormat> dateFormatMap;
	
	static {
		dateFormatMap = new Hashtable<String, DateFormat>();
		
		//yyyy-MM-dd
		//yyyy-M-d
		//HH:mm:ss
		//HH:mm
		//yyyy-MM-dd HH:mm
		//yyyy-MM-dd HH:mm:ss
		//yyyy-M-d HH:mm:ss
		//yyyy-MM-dd'T'HH:mm:ssZ
		//
		//
		//
		//
	}
	
	public static String formatDate(String formatKey, java.util.Date date) throws DataConvertException{
		if(!dateFormatMap.containsKey(formatKey)){
			DateFormat dfDateInstance = new SimpleDateFormat(formatKey);
			dateFormatMap.put(formatKey, dfDateInstance);
		}
		if(dateFormatMap.containsKey(formatKey) && dateFormatMap.get(formatKey)!=null){
			try {
				DateFormat df = dateFormatMap.get(formatKey);
				return df.format(date);
			} catch (Exception e) {
				throw new DataConvertException("格式化日期"+date+"出错, 日期格式"+formatKey);
			}
		}else{
			//TODO  信息属性文件
			throw new DataConvertException("日期格式"+formatKey);
		}
	}
	
	
	
	public static java.util.Date toDate(String formatKey, String dateStr) throws DataConvertException{
		if(!dateFormatMap.containsKey(formatKey)){
			DateFormat dfDateInstance = new SimpleDateFormat(formatKey);
			dateFormatMap.put(formatKey, dfDateInstance);
		}
		if(dateFormatMap.containsKey(formatKey) && dateFormatMap.get(formatKey)!=null){
			try {
				DateFormat df = dateFormatMap.get(formatKey);
				log.info("toDate: ["+formatKey+"] "+dateStr);
				return df.parse(dateStr);
			} catch (Exception e) {
				//TODO 信息属性文件
				throw new DataConvertException("解析日期字符串"+dateStr+"出错, 日期格式"+formatKey);
			}
		}else{
			//TODO  信息属性文件
			throw new DataConvertException("日期格式"+formatKey);
		}
	}



	public static Object toTimestamp(String formatKey, String dateStr) throws DataConvertException {
		if(!dateFormatMap.containsKey(formatKey)){
			DateFormat dfDateInstance = new SimpleDateFormat(formatKey);
			dateFormatMap.put(formatKey, dfDateInstance);
		}
		if(dateFormatMap.containsKey(formatKey) && dateFormatMap.get(formatKey)!=null){
			try {
				DateFormat df = dateFormatMap.get(formatKey);
				log.info("toTimestamp: ["+formatKey+"] "+dateStr);
				return new java.sql.Timestamp(df.parse(dateStr).getTime());
			} catch (Exception e) {
				//TODO 信息属性文件
				throw new DataConvertException("解析日期字符串"+dateStr+"出错, 日期格式"+formatKey);
			}
		}else{
			//TODO  信息属性文件
			throw new DataConvertException("日期格式"+formatKey);
		}
	}



	public static Object toSqlDate(String formatKey, String dateStr) throws DataConvertException {
		if(!dateFormatMap.containsKey(formatKey)){
			DateFormat dfDateInstance = new SimpleDateFormat(formatKey);
			dateFormatMap.put(formatKey, dfDateInstance);
		}
		if(dateFormatMap.containsKey(formatKey) && dateFormatMap.get(formatKey)!=null){
			try {
				DateFormat df = dateFormatMap.get(formatKey);
				log.info("toSqlDate: ["+formatKey+"] "+dateStr);
				return new java.sql.Date(df.parse(dateStr).getTime());
			} catch (Exception e) {
				//TODO 信息属性文件
				throw new DataConvertException("解析日期字符串"+dateStr+"出错, 日期格式"+formatKey);
			}
		}else{
			//TODO  信息属性文件
			throw new DataConvertException("日期格式"+formatKey);
		}
	}



	public static Object toTime(String formatKey, String dateStr) throws DataConvertException {
		if(!dateFormatMap.containsKey(formatKey)){
			DateFormat dfDateInstance = new SimpleDateFormat(formatKey);
			dateFormatMap.put(formatKey, dfDateInstance);
		}
		if(dateFormatMap.containsKey(formatKey) && dateFormatMap.get(formatKey)!=null){
			try {
				DateFormat df = dateFormatMap.get(formatKey);
				log.info("toTime: ["+formatKey+"] "+dateStr);
				return new java.sql.Time(df.parse(dateStr).getTime());
			} catch (Exception e) {
				//TODO 信息属性文件
				throw new DataConvertException("解析日期字符串"+dateStr+"出错, 日期格式"+formatKey);
			}
		}else{
			//TODO  信息属性文件
			throw new DataConvertException("日期格式"+formatKey);
		}
	}



	public static Object toCalendar(String formatKey, String dateStr) throws DataConvertException {
		if(!dateFormatMap.containsKey(formatKey)){
			DateFormat dfDateInstance = new SimpleDateFormat(formatKey);
			dateFormatMap.put(formatKey, dfDateInstance);
		}
		if(dateFormatMap.containsKey(formatKey) && dateFormatMap.get(formatKey)!=null){
			try {
				DateFormat df = dateFormatMap.get(formatKey);
				log.info("toCalendar: ["+formatKey+"] "+dateStr);
				Calendar cal = Calendar.getInstance();
				cal.setTime(df.parse(dateStr));
				return cal;
			} catch (Exception e) {
				//TODO 信息属性文件
				throw new DataConvertException("解析日期字符串"+dateStr+"出错, 日期格式"+formatKey);
			}
		}else{
			//TODO  信息属性文件
			throw new DataConvertException("日期格式"+formatKey);
		}
	}
}
